﻿/*********************************************************************
 * @file       create_dayly.cpp
 * @brief      日程的编辑和新建界面
 * @details    基于ui文件运行
 * @author     南樟(a2169272059@163.com)
 * @date       2025-2-10
 * @version    1.0.0
 * @copyright  a2169272059@163.com
 *********************************************************************/
#include "create_dayly.h"
#include "ui_create_dayly.h"
#include "sqlite.h"
#include "mainwindow.h"

#include <QMessageBox>
/**
 * @brief create_dayly::create_dayly 日程编辑于新建界面的构造函数
 * @param parent 父节点
 */
create_dayly::create_dayly(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::create_dayly)
    , m_id(-1)
{
    ui->setupUi(this);
    // 给不可用组件灰色背景的逻辑在ui文件对应组件的样式表中
    ui->edit_endDate->setEnabled(false);
    ui->btn_del->setEnabled(false);
}
/**
 * @brief create_dayly::change2Edit 切换为编辑模式
 * @param id 对应在数据库中的日程数据的id
 */
void create_dayly::change2Edit(int id)
{
    ui->mode->setText(Mode_edit);
    ui->btn_del->setEnabled(true);
    m_id = id;
    QSqlQuery query(db.get_db());
    query.prepare("SELECT note,begin_time,end_time,begin_date,end_date "
                  "FROM day_duration "
                  "WHERE id = :id");
    query.bindValue(":id",id);
    if(!query.exec()){
        qDebug()<<"读取日程信息出错"<<query.lastError();
        QMessageBox::warning(g_mainWidget,"错误","读取日程信息出错");
        return;
    }
    query.next();
    ui->edit_note->setPlainText(query.value(0).toString());
    ui->timeEdit_begin->setTime(QTime::fromString(query.value(1).toString(),"HH:mm"));
    ui->timeEdit_end->setTime(QTime::fromString(query.value(2).toString(),"HH:mm"));
    ui->edit_beginDate->setDate(QDate::fromString(query.value(3).toString(),"yyyy-MM-dd"));
    ui->edit_endDate->setDate(QDate::fromString(query.value(4).toString(),"yyyy-MM-dd"));
    // 按钮状态更新
    // 全天事件
    ui->cek_daylyItem->setChecked(query.value(1).toString() == "00:00"
                                 && query.value(2).toString() == "23:59");
    on_cek_daylyItem_clicked(ui->cek_daylyItem->isChecked());
    // 同一天结束
    ui->cek_endOnDay->setChecked(query.value(3).toString() == query.value(4).toString());
    on_cek_endOnDay_clicked(ui->cek_endOnDay->isChecked());
}
/**
 * @brief create_dayly::init 根据现在的时间初始化界面的数据
 */
void create_dayly::init()
{
    QDateTime dateTime = QDateTime::currentDateTime();
    ui->edit_beginDate->setDate(dateTime.date());
    ui->edit_endDate->setDate(dateTime.date());
    ui->timeEdit_begin->setTime(dateTime.time());
    ui->timeEdit_end->setTime(dateTime.time().addSecs(3600));
}
/**
 * @brief create_dayly::~create_dayly 日程新建和编辑界面的析构函数
 */
create_dayly::~create_dayly()
{
    delete ui;
}
/**
 * @brief create_dayly::on_btn_cen_clicked [取消]按钮的逻辑
 */
void create_dayly::on_btn_cen_clicked()
{
    emit back2dayly();
}
/**
 * @brief create_dayly::on_cek_endOnDay_clicked [同一天结束]按钮的逻辑
 * @param checked 按钮触发状态
 */
void create_dayly::on_cek_endOnDay_clicked(bool checked)
{
    // 同一天结束
    if(checked){
        ui->edit_endDate->setDate(ui->edit_beginDate->date());
    }
    ui->edit_endDate->setEnabled(!checked);
}

/**
 * @brief create_dayly::on_btn_ok_clicked [确认]按钮的触发逻辑
 */
void create_dayly::on_btn_ok_clicked()
{
    QString begin_time;
    QString end_time;
    // 确认数据
    if(ui->cek_daylyItem->isChecked()){
        begin_time  = "00:00";
        end_time    = "23:59";
    }else{
        begin_time  = ui->timeEdit_begin->time().toString("HH:mm");
        end_time    = ui->timeEdit_end->time().toString("HH:mm");
    }
    QString begin_date = ui->edit_beginDate->date().toString("yyyy-MM-dd");
    QString end_date;
    if(ui->cek_endOnDay->isChecked()){
        end_date = begin_date;
    }else{
        end_date = ui->edit_endDate->date().toString("yyyy-MM-dd");
    }
    // 逻辑检查
    if(begin_date > end_date){
        auto tmpWindow = QMessageBox::warning(this,"逻辑错误"
                                              ,"该日程的开始日期比结束日期更早\n交换他们的位置并返回编辑？"
                                              ,QMessageBox::Yes|QMessageBox::Cancel);
        if(tmpWindow == QMessageBox::Yes){
            // 交换俩日期
            ui->edit_beginDate->setDate(QDate::fromString(end_date,"yyyy-MM-dd"));
            ui->edit_endDate->setDate(QDate::fromString(begin_date,"yyyy-MM-dd"));
        }
        return;
    }else if(begin_date == end_date&&begin_time>end_time){
        auto tmpWindow = QMessageBox::warning(this,"逻辑错误"
                                              ,"该日程的开始时间比结束时间更早\n交换他们的位置并返回编辑？"
                                              ,QMessageBox::Yes|QMessageBox::Cancel);
        if(tmpWindow == QMessageBox::Yes){
            // 交换俩时间
            ui->timeEdit_begin ->setTime(QTime::fromString(end_time,"HH:mm"));
            ui->timeEdit_end->setTime(QTime::fromString(begin_time,"HH:mm"));
        }
        return;
    }
    // 更改数据
    if(ui->mode->text() == Mode_createOne){
        db.insert_day_duration(ui->edit_note->toPlainText()
                               ,begin_date
                               ,end_date
                               ,begin_time
                               ,end_time);
    }else if(ui->mode->text() == Mode_edit){
        QSqlQuery query(db.get_db());
        query.prepare("UPDATE day_duration "
                      "SET note = :note"
                      ", begin_date = :begin_date"
                      ", end_date   = :end_date"
                      ", begin_time = :begin_time"
                      ", end_time   = :end_time "
                      "WHERE id     = :id");
        query.bindValue(":begin_date",begin_date);
        query.bindValue(":end_date",end_date);
        query.bindValue(":begin_time",begin_time);
        query.bindValue(":end_time",end_time);
        query.bindValue(":note",ui->edit_note->toPlainText());
        query.bindValue(":id",m_id);
        if(!query.exec()){
            qDebug()<<"日程数据更新错误"<<query.lastError();
            QMessageBox::warning(g_mainWidget,"错误","日程数据更新错误");
        }
    }else{
        qDebug()<<"非法的日程编辑模式";
    }
    emit back2dayly();
}

/**
 * @brief create_dayly::on_back2Clock_clicked 标题返回逻辑
 */
void create_dayly::on_back2Clock_clicked()
{
    emit back2dayly();
}
/**
 * @brief create_dayly::on_cek_daylyItem_clicked [全天日程]按钮的触发逻辑
 * @param checked 按钮的触发状态
 */
void create_dayly::on_cek_daylyItem_clicked(bool checked)
{
    // 全天日程
    if(checked){
        ui->timeEdit_begin->setTime(QTime::fromString("00:00","HH:mm"));
        ui->timeEdit_end->setTime(QTime::fromString("23:59","HH:mm"));
    }
    ui->timeEdit_begin->setEnabled(!checked);
    ui->timeEdit_end->setEnabled(!checked);
}
/**
 * @brief create_dayly::on_btn_del_clicked [删除]按钮的触发逻辑
 */
void create_dayly::on_btn_del_clicked()
{
    QSqlQuery query(db.get_db());
    query.prepare("DELETE FROM day_duration WHERE id=:id");
    query.bindValue(":id",m_id);
    if(!query.exec()){
        qDebug()<<"删除逻辑出错"<<query.lastError();
        QMessageBox::warning(g_mainWidget,"错误","删除日程数据出错");
    }
    emit back2dayly();
}


